package com.wen.shirodemo;

import com.wen.shirodemo.realm.CustomerRealm;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.mgt.DefaultSecurityManager;
import org.apache.shiro.subject.Subject;

/**
 * 用来测试自定义Realm是否有用
 */
public class TestRealm {
    public static void main(String[] args) {
        //得到安全管理器
        DefaultSecurityManager defaultSecurityManager = new DefaultSecurityManager();
        //设置realm
        defaultSecurityManager.setRealm(new CustomerRealm());
        //给安全工具类设置安全管理器
        //
        SecurityUtils.setSecurityManager(defaultSecurityManager);

        //获取token
        UsernamePasswordToken whw = new UsernamePasswordToken("whw", "123456");

        //得到主体方便后面登录授权
        Subject subject = SecurityUtils.getSubject();

        try{
             subject.login(whw);
            System.out.println("当前类是否应验证通过" + subject.isAuthenticated());
        }catch (UnknownAccountException e){
            e.printStackTrace();
            System.out.println("用户名不正确");
        }catch (IncorrectCredentialsException e){
            e.printStackTrace();
            System.out.println("密码不正确");
        }catch (Exception e){
            e.printStackTrace();
            System.out.println("发生异常");
        }

    }
}
